from django.db import models
from user.models import User
from software.models import SoftwareVersion, Software


# Create your models here.
class SoftwareNeed(models.Model):
    EXAMINESTATE = (
        (0, u'等待审核'),
        (1, u'审核通过'),
        (2, u'审核未通过'),
    )
    NEEDSTATE = (
        (0, u'未处理'),
        (1, u'已处理'),
        (2, u'已驳回'),
    )
    softwareVersion = models.ForeignKey(
        SoftwareVersion,
        verbose_name=u"软件",
        on_delete=models.CASCADE,
        related_name='software_need',
        null=True,
    )
    # software = models.ForeignKey(
    #     Software,
    #     verbose_name=u"软件",
    #     null=True,
    #     on_delete=models.CASCADE,
    #     related_name='softwareneed_need',
    # )
    needState = models.CharField(
        verbose_name=u'新增需求',
        choices=NEEDSTATE,
        max_length=20,
        default=u'0',
    )
    examineState = models.CharField(
        verbose_name=u'需求审核状态',
        choices=EXAMINESTATE,
        max_length=20,
        default=u'0',
    )
    user = models.ForeignKey(
        User,
        verbose_name=u"提出者",
        on_delete=models.CASCADE,
        related_name='software_presenter',
    )
    title = models.CharField(
        verbose_name=u'需求标题',
        max_length=50,
        default=u'',
    )
    needContent = models.CharField(
        verbose_name=u'需求内容',
        max_length=2000,
        default=u'',
    )
    # needResult = models.CharField(
    #     max_length=70,
    #     verbose_name=u'需求处理结果',
    #     default=u'',
    #     null=True
    # )
    raiseTime = models.DateTimeField(
        verbose_name=u"提出时间",
        auto_now_add=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    isExamine = models.BooleanField(
        verbose_name=u'是否审核',
        default=False,
    )
    ishandle = models.BooleanField(
        verbose_name=u'是否处理',
        default=False,
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    def __unicode__(self):
        return self.software

    class Meta:
        ordering = ('createTime',)


class NeedHandle(models.Model):
    need = models.ForeignKey(
        SoftwareNeed,
        verbose_name=u"软件需求",
        on_delete=models.CASCADE,
        related_name='need_handle',
    )
    handleContent = models.CharField(
        verbose_name=u'回复内容',
        max_length=2000,
        default=u'',
    )
    rejectResult = models.CharField(
        max_length=2000,
        verbose_name=u'需求处理结果',
        default=u'',
        null=True
    )
    user = models.ForeignKey(
        User,
        verbose_name=u"回复者",
        on_delete=models.CASCADE,
        related_name='need_handler',
        null=True,
    )
    handleTime = models.DateTimeField(
        verbose_name=u"回复时间",
        auto_now_add=True,
    )
    createTime = models.DateTimeField(
        auto_now_add=True,
        verbose_name=u"创建时间",
    )
    isActive = models.BooleanField(
        verbose_name=u'是否活跃',
        default=True,
    )

    def __unicode__(self):
        return self.need

    class Meta:
        ordering = ('createTime',)
